// 网络请求
import {
	baseUrl
} from '../common/config/config.js'
export default function requst(options) {
	console.log('这里执行', );
	return new Promise((resolve, reject) => {
		const {
			url,
			data,
			method,
			loading = false,
			loadingText = "加载中"
		} = options;
		let contentType = "";
		if (method.toLocaleLowerCase() === 'POST') {
			contentType = "application/json"
		}
		if (loading) {
			uni.showLoading({
				title: loadingText ?? '加载中'
			});
		}
		uni.request({
			url: baseUrl + options.url,
			data: options.data,
			method: options.method.toLocaleLowerCase(),
			timeout: 5 * 1000,
			header: {
				'content-type': contentType
			},
			success: (res) => {
				if (res.statusCode < 200 || res.statusCode > 300) {
					uni.showToast({
						title: res.data.msg,
						duration: 2000,
						icon: "error"
					});
				}
				resolve(res);
				if(loading){
					uni.hideLoading()
				}
			
			},
			fail: (err) => {
				reject(err);
				if (loading) {
					uni.hideLoading();
				}

			}
		});
	})
}
